package com.ttt.servicer.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.ttt.mapper.EquipmentMapper;
import com.ttt.mapper.HealthMapper;
import com.ttt.pojo.Equipment;
import com.ttt.pojo.Soil;
import com.ttt.servicer.SoilHealth;
import com.ttt.util.Result;
import com.ttt.util.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SoilHealthImpl implements SoilHealth {

    @Autowired
    private HealthMapper healthMapper;

    @Autowired
    private EquipmentMapper equipmentMapper;

    //获取土壤健康基本信息
    @Override
    public Result findSoilHealthInfo() throws JsonProcessingException {

        QueryWrapper<Soil> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("pH"," humidity","organic","potassium","phosphorus","nitrogen");
        List<Map<String, Object>> list = healthMapper.selectMaps(queryWrapper);

        //将数据封装成json
        Result<String> result = Result.toJson(list, ResultCodeEnum.SUCCESS);
        System.out.println("土壤基本信息：" + result.getData());

        return result;
    }


    //获取设备在线与掉线数
    @Override
    public Result findOlineEquip() throws JsonProcessingException {

        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("status", "COUNT(*) as count")
                .groupBy("status");
        List<Map<String, Object>> list = equipmentMapper.selectMaps(queryWrapper);

        Result<String> result = Result.toJson(list, ResultCodeEnum.SUCCESS);
        System.out.println("设备状态统计：" + result.getData());

        return result;
    }
}
